//package com.hxm.demo.transaction;
//
//import com.google.protobuf.ServiceException;
//import com.hxm.demo.been.Person;
//import org.apache.ibatis.session.SqlSession;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.transaction.TransactionDefinition;
//import org.springframework.transaction.TransactionStatus;
//import org.springframework.transaction.support.DefaultTransactionDefinition;
//
//import javax.annotation.Resource;
//import java.sql.Connection;
//import java.sql.SQLException;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.Callable;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.atomic.AtomicBoolean;
//
///**
// * description: add a description
// *
// * @author huangxiaoming
// * @version 1.0.0
// * @date 2023/03/30 17:39:18
// */
//public class HandCommitTransaction {
//
//    @Autowired
//    private DataSourceTransactionManager dataSourceTransactionManager;
//    /**
//     * 测试多线程事务.
//     */
//
//    public void saveThread() throws SQLException {
//        DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
//        defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
//        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition);
//        try {
//            //获取执行器
//            ExecutorService service = Executors.newFixedThreadPool(10);
//            List<Callable<Integer>> callableList  = new ArrayList<>();
//            //拆分list
//            List<List<Person>> lists=averageAssign(employeeDOList, 5);
//            AtomicBoolean atomicBoolean = new AtomicBoolean(true);
//            for (int i =0;i<lists.size();i++){
//                if (i==lists.size()-1){
//                    atomicBoolean.set(false);
//                }
//                List<EmployeeDO> list  = lists.get(i);
//                //使用返回结果的callable去执行,
//                Callable<Integer> callable = () -> {
//                    //让最后一个线程抛出异常
//                    if (!atomicBoolean.get()){
//                        throw new ServiceException("001","出现异常");
//                    }
//                    return employeeMapper.saveBatch(list);
//                };
//                callableList.add(callable);
//            }
//            //执行子线程
//            List<Future<Integer>> futures = service.invokeAll(callableList);
//            for (Future<Integer> future:futures) {
//                //如果有一个执行不成功,则全部回滚
//                if (future.get()<=0){
//                    connection.rollback();
//                    return;
//                }
//            }
//            dataSourceTransactionManager.commit(transaction);
//            System.out.println("添加完毕");
//        }catch (Exception e){
//            connection.rollback();
//            log.info("error",e);
//            throw new ServiceException("002","出现异常");
//        }finally {
//            connection.close();
//        }
//    }
//}
